﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CellarDoor.Gui.Elements;
using Microsoft.Xna.Framework;

namespace CellarDoor.Gui.Elements
{
    public class UiFrame : UiPanel
    {
        /// <summary>
        /// Occurs before the layer is shown
        /// </summary>
        public event EventHandler ShowBefore;

        /// <summary>
        /// Occurs after the layer is shown
        /// </summary>
        public event EventHandler ShowAfter;

        /// <summary>
        /// Occurs after the layer is closed
        /// </summary>
        public event EventHandler CloseBefore;

        /// <summary>
        /// Occurs after the layer is closed
        /// </summary>
        public event EventHandler CloseAfter;

        private bool fullScreen = true;
        /// <summary>
        /// Gets and sets a value indicating whether this frame is in fullscreen or not
        /// </summary>
        public bool FullScreen
        {
            get { return fullScreen; }
            set { fullScreen = value; }
        }

        // TODO: make the UiFrame resizeable
        //private bool resizeable = true;
        ///// <summary>
        ///// Gets and sets a value indicating whether this frame is resizeable or not
        ///// </summary>
        //public bool Resizeable
        //{
        //    get { return resizeable; }
        //    set { resizeable = value; }
        //}

        /// <summary>
        /// Creates a new element layer
        /// </summary>
        /// <param name="manager"></param>
        public UiFrame(UiManager manager)
            : base()
        {
            Initialize();
            SetManager(manager);
        }

        private void Initialize()
        {
            Padding = new UiPadding(30, 0, 0, 0);
            ElementName = "Frame";
        }

        protected virtual void OnShowBefore()
        {
            if (ShowBefore != null)
            {
                ShowBefore(this, EventArgs.Empty);
            }
        }

        protected virtual void OnShowAfter()
        {
            if (ShowAfter != null)
            {
                ShowAfter(this, EventArgs.Empty);
            }
        }

        protected virtual void PerformShow()
        {
            Manager.AddScreen(this);
        }

        public virtual void Show()
        {
            OnShowBefore();
            PerformShow();
            OnShowAfter();
        }

        protected virtual void OnCloseBefore()
        {
            if (CloseBefore != null)
            {
                CloseBefore(this, EventArgs.Empty);
            }
        }

        protected virtual void OnCloseAfter()
        {
            if (CloseAfter != null)
            {
                CloseAfter(this, EventArgs.Empty);
            }
        }

        protected virtual void PerformClose()
        {
            Manager.RemoveScreen(this);
        }

        public virtual void Close()
        {
            OnCloseBefore();
            PerformClose();
            OnCloseAfter();
        }

        public override void Update(GameTime gameTime)
        {
            if (fullScreen)
            {
                Top = 0;
                Left = 0;
                Width = Manager.NativeViewport.Width;
                Height = Manager.NativeViewport.Height;
                //SizeScaleAxis = UiScaleAxis.Both;
                //LocationScaleAxis = UiScaleAxis.Both;
            }

            base.Update(gameTime);
        }
    }
}
